<template>
    <view :class="[jClass, 'jk-modal', {'jk-modal-show': visible}]">
        <view :class="['jk-modal-mask', 'jk-class-mask', {'jk-modal-mask-show': visible}]"></view>
        <view class="jk-modal-main">
            <view class="jk-modal-content">
                <view class="jk-modal-title" v-if="title">{{ title }}</view>
                <view class="jk-modal-body">
                    <slot></slot>
                </view>
                
                <view class="jk-modal-actions" v-if="!actions.length">
                    <view>
                        <jk-grid v-if="showOk || showCancel">
                            <jk-grid-item v-if="showCancel" jk-class="jk-modal-grid-item" width="50%">
                                <button class="jk-modal-btn-cancel" size="default" plain="true" @click="handleClickCancel">{{ cancelText }}</button>
                            </jk-grid-item>
                            <jk-grid-item v-if="showOk" jk-class="jk-modal-grid-item" width="50%">
                                <button class="jk-modal-btn-ok" size="default" plain="true" @click="handleClickOk">{{ okText }}</button>
                            </jk-grid-item>
                        </jk-grid>
                    </view>
                    
                </view>
                <view class="jk-modal-actions" v-else>
                    <block v-if="actionMode === 'horizontal'">
                        <jk-grid jk-class="jk-modal-grid">
                            <jk-grid-item v-for="(item, i) in actions" :key="i">
                                <button @click="handleClickItem" :data-index="i" plain="true">
                                    <view class="jk-modal-btn-loading" v-if="item.loading"></view>
                                    <jk-icon v-if="item.icon" :type="item.icon"></jk-icon>
                                    <view class="jk-modal-btn-text" :style="item.color ? 'color: ' + item.color : ''">{{ item.name }}</view>
                                </button>
                            </jk-grid-item>
                        </jk-grid>
                    </block>
                    <block v-else>
                        <view class="jk-modal-action-vertical" v-for="(item, index) in actions" :key="item.name">
                            <button @click="handleClickItem" plain="true" :data-index="index">
                                <view v-if="item.loading"></view>
                                <jk-icon v-if="item.icon" :type="item.icon"></jk-icon>
                                <text>{{item.name}}</text>
                            </button>
                        </view>
                    </block>
                </view>
            </view>
        </view>

    </view>
</template>

<script>
    import JkGrid from '../../grid/index.js'
    import JkGridItem from '../../grid-item/index.js'
    import JkIcon from '../../icon/index.js'
    export default {
        name: 'Modal',
        props: {
            visible: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: ''
            },
            showOk: {
                type: Boolean,
                default: true
            },
            showCancel: {
                type: Boolean,
                default: true
            },
            okText: {
                type: String,
                default: '确定'
            },
            cancelText: {
                type: String,
                default: '取消'
            },
            // 按钮组，有此值时，不显示 ok 和 cancel 按钮
            actions: {
                type: Array,
                default () {
                    return []
                }
            },
            // horizontal || vertical
            actionMode: {
                type: String,
                default: 'horizontal'
            },
            jClass: {
                type: String,
                default: ''
            }
        },
        data(){
            return {
                
            }
        },
        components: { JkGrid, JkGridItem, JkIcon },
        created(){
            console.log(`this is showOk:${this.showOk}`,`this is showCancel:${this.showCancel}`)
        },
        methods: {
            handleClickItem({ currentTarget = {} }) {
                const dataset = currentTarget.dataset || {};
                const { index } = dataset;
                this.$emit('click', index);
            },
            handleClickOk() {
                this.$emit('ok');
            },
            handleClickCancel() {
                this.$emit('cancel');
            }
        }
    }
</script>

<style>
    .jk-modal {
        position: fixed;
        overflow: hidden;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        height: 100%;
        z-index: 1000;
        display: flex;
        outline: 0;
        -webkit-box-align: center;
        align-items: center;
        -webkit-box-pack: center;
        justify-content: center;
        transform: translateZ(1px);
        opacity: 0;
        visibility: hidden;
    }

    .jk-modal-show {
        visibility: visible;
        opacity: 1;
    }

    .jk-modal-mask {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, .7);
        z-index: 1000;
        transition: all .2s ease-in-out;
        opacity: 0;
        visibility: hidden;
    }

    .jk-modal-mask-show {
        opacity: 1;
        visibility: visible;
    }

    .jk-modal-main {
        width: 270px;
        position: relative;
        z-index: 1001;
    }

    .jk-modal-content {
        border-radius: 7px;
        padding-top: 15px;
        position: relative;
        background-color: #fff;
        border: 0;
        background-clip: padding-box;
        text-align: center;
        height: 100%;
        overflow: hidden;
    }

    .jk-modal-body {
        max-height: 100px;
        margin-bottom: 15px;
        font-size: 14px;
        color: #80848f;
        height: 100%;
        line-height: 1.5;
        overflow: auto;
    }

    .jk-modal-title {
        padding: 6px 15px 15px;
        margin: 0;
        font-size: 18px;
        line-height: 1;
        color: #1c2438;
        text-align: center;
    }

    .jk-modal-actions {
        margin: 0 1px;
    }

    .jk-modal-action-vertical {
        position: relative;
    }

    .jk-modal-action-vertical:after {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 200%;
        height: 200%;
        transform: scale(.5);
        transform-origin: 0 0;
        pointer-events: none;
        box-sizing: border-box;
        border: 0 solid #e9eaec;
        border-top-width: 1px;
    }

    .jk-modal-grid {
        border-radius: 0 0 7px 7px;
        border-left: none;
    }

    .jk-modal-grid-item,
    .jk-modal-grid-item-last {
        padding: 0;
        border-bottom: none;
    }

    .jk-modal-grid-item-last {
        border-right: none;
    }

    .j-modal-btn-ok {
        color: #2d8cf0 !important;
    }

    .j-modal-btn-loading {
        display: inline-block;
        vertical-align: middle;
        margin-right: 10px;
        width: 12px;
        height: 12px;
        background: 0 0;
        border-radius: 50%;
        border: 2px solid #e5e5e5;
        border-color: #666 #e5e5e5 #e5e5e5 #e5e5e5;
        animation: btn-spin .6s linear;
        animation-iteration-count: infinite;
    }

    .j-modal-btn-text {
        display: inline-block;
        vertical-align: middle;
    }

    .j-modal-btn-icon {
        font-size: 14px !important;
        margin-right: 4px;
    }

    @keyframes btn-spin {
        0% {
            transform: rotate(0);
        }

        100% {
            transform: rotate(360deg);
        }
    }
</style>
